package 力扣;

/**
 给定有序数组: [-10,-3,0,5,9],
 一个可能的答案是：[0,-3,9,-10,null,5]，它可以表示下面这个高度平衡二叉搜索树：
    0
   / \
  -3   9
  /   /
 -10  5
 */

public class _108将有序数组转换为二叉搜索树 {

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode() {}
        TreeNode(int val) { this.val = val; }
        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    public TreeNode sortedArrayToBST(int[] nums) {
       return  toBst(nums,0,nums.length-1);
    }

    private TreeNode toBst(int[] nums, int start, int end) {
        if( start > end ) return null;
        int mid = (start + end ) >> 1;
        TreeNode node = new TreeNode(nums[mid]);

        node.left = toBst(nums,start,mid-1);
        node.right = toBst(nums,mid+1,end);
        return node;
    }


}
